package com.cgw.game.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.cgw.game.R;
import com.cgw.game.view.MyCombinedChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DoubleLineBarActivity extends AppCompatActivity {
    private MyCombinedChart chart;
    String[] titles = new String[]{"深睡","浅睡","清醒","小憩"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_double_line_bar);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle(getIntent().getStringExtra("title"));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//返回
            }
        });
        chart = findViewById(R.id.chart);
        initBarLineChart();

        List<Entry> values1 = new ArrayList<>();
        List<BarEntry> values2 = new ArrayList<>();



        values1.add(new Entry(0, 95));
        values1.add(new Entry(1, 17));
        values1.add(new Entry(2, 23));
        values1.add(new Entry(3, 16));
        values1.add(new Entry(4, 23));
        values1.add(new Entry(5, 14));
        values1.add(new Entry(6, 16));
        values1.add(new Entry(7, 22));
        values1.add(new Entry(8, 22));
        values2.add(new BarEntry(0,40));
        values2.add(new BarEntry(1,40));
        values2.add(new BarEntry(2,40));
        values2.add(new BarEntry(3,40));
        values2.add(new BarEntry(4,40));
        values2.add(new BarEntry(5,40));
        values2.add(new BarEntry(6,40));
        values2.add(new BarEntry(7,40));
        values2.add(new BarEntry(8,40));

        setData(values1,values2);

    }
    private void initBarLineChart() {
        chart.setExtraBottomOffset(5f);
        chart.setTouchEnabled(true);
        chart.setPinchZoom(false);
        chart.setScaleYEnabled(false);
        chart.setDragEnabled(true);
        chart.setDoubleTapToZoomEnabled(false);
        chart.getViewPortHandler().setMaximumScaleX(5.0f);
        //取消说明
        chart.getLegend().setEnabled(false);
        chart.getDescription().setEnabled(false);
        chart.setNoDataText("暂无数据");


        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
        leftAxis.setTextSize(12f);
        leftAxis.setTextColor(Color.parseColor("#91a2a3"));
//      leftAxis.setValueFormatter(new PercentFormatter());
//        leftAxis.setValueFormatter(new IndexAxisValueFormatter());
        leftAxis.setEnabled(true);
        leftAxis.setGridColor(Color.parseColor("#e3edf2"));
        leftAxis.setDrawAxisLine(false);
        leftAxis.setAxisMinimum(0f);
        leftAxis.setAxisMaximum(150f);
        /*leftAxis.addLimitLine(new LimitLine(125));
        leftAxis.addLimitLine(new LimitLine(147));
        leftAxis.addLimitLine(new LimitLine(188));*/
        leftAxis.setLabelCount(5, true);



        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setTextColor(Color.RED);
        rightAxis.setAxisMaximum(40);
        rightAxis.setAxisMinimum(0);
        rightAxis.setLabelCount(9, true);
        rightAxis.setDrawGridLines(false);
        rightAxis.setGranularityEnabled(false);
        rightAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                switch ((int)value){
                    case 5:return titles[0];
                    case 15:return titles[1];
                    case 25:return titles[2];
                    case 35:return titles[3];
                }
                return "";
            }
        });


        //横向轴线设置
        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(12f);
        xAxis.setTextColor(Color.parseColor("#91a2a3"));
        xAxis.setEnabled(true);  //是否打开x轴
        xAxis.setDrawGridLines(false);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularityEnabled(true);
        xAxis.setLabelCount(5, false);
        // 转换要显示的标签文本，value值默认是int从0开始
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return famatTime((int) value);
            }
        });
        xAxis.setAxisLineColor(Color.parseColor("#e3edf2"));
        //设置为虚线
        xAxis.setDrawAxisLine(true);
    }


    private String famatTime(int value) {
        int min = value * 5;
        String m = min / 60 + "";
        if (min / 60 < 10) {
            m = "0" + min / 60;
        }
        String s = min % 60 + "";
        if (min % 60 < 10) {
            s = "0" + min % 60;
        }
        return m + ":" + s;
    }



    private void setData(List<Entry> values1,List<BarEntry> values2) {
        chart.clear();
        LineDataSet lineDataSet1;
        lineDataSet1 = new LineDataSet(values1, "心率");
        lineDataSet1.setAxisDependency(YAxis.AxisDependency.LEFT);
        lineDataSet1.setMode(LineDataSet.Mode.LINEAR);
        lineDataSet1.setColor(getResources().getColor(R.color.red));
        lineDataSet1.setCircleColor(getResources().getColor(R.color.red));
        lineDataSet1.setDrawHorizontalHighlightIndicator(false);
        lineDataSet1.setHighLightColor(getResources().getColor(R.color.red));
        lineDataSet1.enableDashedHighlightLine(10, 10, 0);
        lineDataSet1.setFillDrawable(getResources().getDrawable(R.drawable.line_chart_bg));
        lineDataSet1.setFillAlpha(100);
        lineDataSet1.setDrawFilled(true);
        lineDataSet1.setHighlightLineWidth(1f);
        lineDataSet1.setLineWidth(1f);
        lineDataSet1.setCircleRadius(3f);
        lineDataSet1.setDrawCircleHole(false);
        lineDataSet1.setDrawCircles(false);
        lineDataSet1.setDrawValues(false);
        LineData lineData = new LineData(lineDataSet1);
        CombinedData data = new CombinedData();
        data.setData(lineData);

        BarDataSet barDataSet = new BarDataSet(values2, "睡眠"); // 新建一组柱形图，"LAR"为本组柱形图的Label
        barDataSet.setColor(getResources().getColor(R.color.blue_21)); // 设置柱形图颜色
        //不显示值
        barDataSet.setDrawValues(false);
        barDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
        barDataSet.setBarBorderWidth(1f);
        barDataSet.setBarBorderColor(Color.parseColor("#62A0F3"));
        BarData barData = new BarData();
        barData.addDataSet(barDataSet);// 添加一组柱形图，如果有多组柱形图数据，则可以多次addDataSet来设置
        barData.setBarWidth(1f);
        data.setData(barData);
        chart.setData(data);
    }
}
